package com.gcph.zhuoba.main.Fragment;

import java.util.ArrayList;
import java.util.HashMap;

import com.gcph.zhuoba.R;
import com.gcph.zhuoba.main.activitys.MainActivity;
import com.gcph.zhuoba.main.activitys.ResourceDetailActivity;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

/**
 * 台球圈-消息首页
 * 
 * @author heroz
 */
public class CircleJiaoanFragment extends Fragment {
	private GridView  gridview;
	private int images[]=new int[]{R.drawable.item_jiaoan_zouwei,R.drawable.item_jiaoan_kqiu,R.drawable.item_jiaoan_jieqiu,R.drawable.item_jiaoan_fangshou};

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View circleJiaoanLayout = inflater.inflate(R.layout.layout_circle_jiaoan, container, false);
		//初始化头部按钮
		initBtnGuangchang(circleJiaoanLayout);
		
		// 初始化头部按钮
		initBtnGanhuo(circleJiaoanLayout);
		
		//初始化教案GridView
		 gridview = (GridView) circleJiaoanLayout.findViewById(R.id.gridview_jiaoan);

         ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();
         for (int i = 0; i < images.length; i++) {
             HashMap<String, Object> map = new HashMap<String, Object>();
             map.put("itemImage", images[i]);
             lstImageItem.add(map);
         }
         
         SimpleAdapter saImageItems = new SimpleAdapter(circleJiaoanLayout.getContext(), lstImageItem, R.layout.item_jiaoan_grid, new String[]{"itemImage", "itemText"}, new int[]{R.id.itemImage});
         gridview.setAdapter(saImageItems);
         gridview.setOnItemClickListener(new ItemClickListener());
         
		return circleJiaoanLayout;
	}
	
	/**
	 * 单击grid的一个单元格
	 * @author zhanghejie
	 */
	class ItemClickListener implements AdapterView.OnItemClickListener {
        public void onItemClick(AdapterView<?> parent, View view, int position, long rowid) {
        	// 跳转到详情页
			Intent intent = new Intent(getActivity(), ResourceDetailActivity.class);
			intent.putExtra("resourceId", position+1+"");
			//内置固定的教案，加一个预览按钮
			intent.putExtra("flag", "neizhi");
			startActivity(intent);
        }
    }
	/**
	 * 初始化广场按钮点击事件
	 * @param circleMessageLayout
	 */
	public void initBtnGuangchang(View circleMessageLayout){
		Button btnGuangchang=(Button)circleMessageLayout.findViewById(R.id.btn_guangchang);
		btnGuangchang.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//点击消息按钮跳转到消息首页
				FragmentManager fManager=getFragmentManager();
				FragmentTransaction fTransaction=fManager.beginTransaction();
				((MainActivity)getActivity()).hideFragments(fTransaction);
				CircleFragment circleFragment=new CircleFragment();
				fTransaction.replace(R.id.content, circleFragment, "circleFragment");
				fTransaction.commit();
			}
		});
	}
	
	/**
	 * 初始化消息按钮点击事件
	 * 
	 * @param circleLayout
	 */
	public void initBtnGanhuo(View circleLayout) {
		Button btnGanhuo = (Button) circleLayout.findViewById(R.id.btn_ganhuo);
		btnGanhuo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 点击干货按钮跳转到干货首页
				FragmentManager fManager = getFragmentManager();
				FragmentTransaction fTransaction = fManager.beginTransaction();
				((MainActivity) getActivity()).hideFragments(fTransaction);
				CircleGanhuoFragment circleGanhuoFragment = new CircleGanhuoFragment();
				fTransaction.replace(R.id.content, circleGanhuoFragment, "circleGanhuoFragment");
				fTransaction.commit();
			}
		});
	}

}